package cc.jq1024.domain.credit.model.aggregate;

import cc.jq1024.domain.award.model.valobj.TaskStateVO;
import cc.jq1024.domain.credit.event.CreditAdjustSuccessMessageEvent;
import cc.jq1024.domain.credit.model.entity.CreditAccountEntity;
import cc.jq1024.domain.credit.model.entity.CreditOrderEntity;
import cc.jq1024.domain.credit.model.entity.CreditTaskEntity;
import cc.jq1024.domain.credit.model.valobj.TradeNameVO;
import cc.jq1024.domain.credit.model.valobj.TradeTypeVO;
import cc.jq1024.types.event.BaseEvent;
import cc.jq1024.types.snow.SnowflakeIdGenerator;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

/**
 * 积分交易聚合对象
 *
 * @author li--jiaqiang
 * @date 2024−07−16
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TradeAggregate {

    /**
     * 用户ID
     */
    private String userId;

    /**
     * 积分订单对象
     */
    private CreditOrderEntity creditOrderEntity;

    /**
     * 积分账户对象
     */
    private CreditAccountEntity creditAccountEntity;

    /**
     * 任务实体 - 补偿 MQ 消息
     */
    private CreditTaskEntity taskEntity;


    /**
     * 构建积分账户对象
     */
    public static CreditAccountEntity createCreditAccountEntity(String userId, BigDecimal adjustAmount) {
        return CreditAccountEntity.builder().userId(userId).adjustAmount(adjustAmount).build();
    }

    /**
     * 构建积分账户对象
     */
    public static CreditOrderEntity createCreditOrderEntity(String userId, TradeNameVO tradeName,
                                                                TradeTypeVO tradeType,
                                                                BigDecimal tradeAmount, String outBusinessNo) {
        return CreditOrderEntity.builder()
                .userId(userId)
                .orderId(String.valueOf(new SnowflakeIdGenerator().nextId()))
                .tradeName(tradeName)
                .tradeType(tradeType)
                .tradeAmount(tradeAmount)
                .outBusinessNo(outBusinessNo)
                .build();
    }

    /**
     * 构建任务实体对象
     */
    public static CreditTaskEntity buildCreditTaskEntity(String userId, String topic, String messageId,
                                                         BaseEvent.EventMessage<CreditAdjustSuccessMessageEvent.CreditAdjustSuccessMessage> message) {
        CreditTaskEntity taskEntity = new CreditTaskEntity();
        taskEntity.setUserId(userId);
        taskEntity.setTopic(topic);
        taskEntity.setMessageId(messageId);
        taskEntity.setMessage(message);
        taskEntity.setState(TaskStateVO.create);
        return taskEntity;
    }
}